***************************************
*Figure 2: Robots and schooling by age*
***************************************

*Education
use "$final_data_outcomes/czone_Students.dta", clear

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports 
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", keep(1 3) nogenerate

*Keep period of interest
keep if year<=2000

xtset czone year

foreach var in public private uniTop30Dummy largest150 anyAid {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)


*Create confidence intervals
gen coef =.
gen se =.
gen ci_u =.
gen ci_d =.


*Regression coefficients
local i = 1
foreach age in a19_24 a25_29 a30_34 a35_39 a40_44 a45_49 a50_54 a55_59 a60_64 {

eststo: ivreghdfe d_sc_`age' (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

replace coef = _b[expof_us_adj] in `i'
replace se = _se[expof_us_adj] in `i'
replace ci_u = coef + 1.96*se in `i'
replace ci_d = coef - 1.96*se in `i'

local i = `i'+1

}

keep if _n<=9
keep coef* se* ci*

/*
gen position=0 if _n==1
replace position=2  if _n==2
replace position=4  if _n==3
replace position=6  if _n==4
replace position=8  if _n==5
replace position=10  if _n==6
replace position=12  if _n==7
replace position=14  if _n==8
replace position=16  if _n==9

*Figure: robots and schooling by age

twoway  (scatter coef position, color(black) msymbol(diamond) msize(small)) ///
(line coef position, color(black*0.75) lwidth(vthin)) ///
		(rcap ci_u ci_d position, lcolor(black) lwidth(vthin)) ///
		, ylabel(-.2(.2)1.6, angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(2) ytitle("US robot exposure" "point estimate", size(medium)) ///
		xscale(noline) xscale(alt) xlabel(-1 " " 0 `" "19-24" " " "[1]" "' 2 `" "25-29" " " "[2]" "' 4 `" "30-34" " " "[3]" "' 6 `" "35-39" " " "[4]" "' 8 `" "40-44" " " "[5]" "' 10 `" "45-49" " " "[6]" "' 12 `" "50-54" " " "[7]" "' 14 `" "55-59" " " "[8]" "' 16 `" "60-64" " " "[9]" "' 17 " ", labsize(medium) noticks) ///
		text(1.85 0 "______", place(c) size(medium) color(black)) ///
		text(1.85 2 "______", place(c) size(medium) color(black)) ///
		text(1.85 4 "______", place(c) size(medium) color(black)) ///
		text(1.85 6 "______", place(c) size(medium) color(black)) ///
		text(1.85 8 "______", place(c) size(medium) color(black)) ///
		text(1.85 10 "______", place(c) size(medium) color(black)) ///
		text(1.85 12 "______", place(c) size(medium) color(black)) ///
		text(1.85 14 "______", place(c) size(medium) color(black)) ///
		text(1.85 16 "______", place(c) size(medium) color(black)) ///
		graphregion(color(white) fcolor(white) margin(r+20))
		graph export "$figures_main/Figure 2.png", replace
*/		


*** narrow 

gen position=0 if _n==1
replace position=2  if _n==2
replace position=4  if _n==3
replace position=6  if _n==4
replace position=8  if _n==5
replace position=10  if _n==6
replace position=12  if _n==7
replace position=14  if _n==8
replace position=16  if _n==9

*Figure: robots and schooling by age

twoway  (bar coef position, barwidth(1.2) fcolor(navy) msymbol(diamond) msize(small)) ///
		(scatter coef position, color(black) msymbol(diamond) msize(small)) ///
		(line coef position, color(black*0.75) lwidth(vthin)) ///
		(rcap ci_u ci_d position, lcolor(black) lwidth(vthin)) ///
		, ylabel(-.2(.2)1.6, angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(3.5) ytitle("US robot exposure" "point estimate", size(medium)) ///
		xscale(noline) xscale(alt) xlabel(-1 " " 0 `" "19-24" " " "[1]" "' 2 `" "25-29" " " "[2]" "' 4 `" "30-34" " " "[3]" "' 6 `" "35-39" " " "[4]" "' 8 `" "40-44" " " "[5]" "' 10 `" "45-49" " " "[6]" "' 12 `" "50-54" " " "[7]" "' 14 `" "55-59" " " "[8]" "' 16 `" "60-64" " " "[9]" "' 17 " ", labsize(medium) noticks) ///
		text(1.85 0 "______", place(c) size(medium) color(black)) ///
		text(1.85 2 "______", place(c) size(medium) color(black)) ///
		text(1.85 4 "______", place(c) size(medium) color(black)) ///
		text(1.85 6 "______", place(c) size(medium) color(black)) ///
		text(1.85 8 "______", place(c) size(medium) color(black)) ///
		text(1.85 10 "______", place(c) size(medium) color(black)) ///
		text(1.85 12 "______", place(c) size(medium) color(black)) ///
		text(1.85 14 "______", place(c) size(medium) color(black)) ///
		text(1.85 16 "______", place(c) size(medium) color(black)) ///
		graphregion(color(white) fcolor(white) margin(r+5))
		graph export "$figures_main/Figure 2a.png", replace

		
		
		
		
